package com.edu.framework.websocket.handler;

import cn.hutool.core.util.StrUtil;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.card.CardRequest;
import com.edu.framework.websocket.message.card.TapRecordsRequest;
import com.edu.framework.websocket.service.CardService;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class TapRecordsRequestHandler implements IHandler<TapRecordsRequest> {

    @Resource
    private CardService wsCardService;

    @Override
    public void execute(WebSocketSession session, TapRecordsRequest message) {
        if (message == null) {
            return;
        }
        Map<String, String> data = message.getData();
        String text = data.get(TapRecordsRequest.RESP_KEY);
        Response response = null;

        if (StrUtil.isBlank(text)){
            response = Response.fail(CardRequest.TYPE, "刷卡记录为空");
        } else {
            response = wsCardService.diffCharge(text);
        }

//        try {
//            WebSocketHolder.send(session,response);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
    }

    @Override
    public String getType() {
        return TapRecordsRequest.TYPE;
    }




}
